package com.ruoyi.door.service;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.easy.query.core.proxy.sql.Select;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.door.controller.admin.orderdetail.vo.DoorOrderDetailPageReqVO;
import com.ruoyi.door.controller.admin.orderdetail.vo.DoorOrderDetailRespVO;
import com.ruoyi.door.controller.admin.orderdetail.vo.proxy.DoorOrderDetailRespVOProxy;
import com.ruoyi.door.domain.DoorHouse;
import com.ruoyi.door.domain.DoorOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.door.domain.proxy.DoorOrderDetailProxy;
import com.ruoyi.door.domain.DoorOrderDetail;
import com.easy.query.api.proxy.entity.select.EntityQueryable;
import com.easy.query.core.api.pagination.EasyPageResult;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.easy.query.api.proxy.client.EasyEntityQuery;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class DoorOrderDetailService {
    @Autowired
    private EasyEntityQuery easyEntityQuery;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    public DoorOrderDetail selectDoorOrderDetailById(Long id) {
        return easyEntityQuery.queryable(DoorOrderDetail.class).where(x -> x.id().eq(id))
                .firstOrNull();
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param doorOrderDetail 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    public EasyPageResult<DoorOrderDetailRespVO> selectDoorOrderDetailPageList(DoorOrderDetailPageReqVO doorOrderDetail) {
        return buildQuery(doorOrderDetail, null).toPageResult(TableSupport.buildPageRequest().getPageNum(),
                TableSupport.buildPageRequest().getPageSize());
    }

    public List<DoorOrderDetailRespVO> selectDoorOrderDetailList(DoorOrderDetailPageReqVO doorOrderDetail, Long userId) {
        List<DoorOrderDetailRespVO> pageResult = buildQuery(doorOrderDetail, userId).toList();
        return pageResult;
    }

    private EntityQueryable<DoorOrderDetailRespVOProxy, DoorOrderDetailRespVO> buildQuery(DoorOrderDetailPageReqVO doorOrderDetail, Long userId) {
        EntityQueryable<DoorOrderDetailRespVOProxy, DoorOrderDetailRespVO> select =easyEntityQuery.queryable(DoorOrderDetail.class)
                .leftJoin(DoorOrder.class,(detail,order)-> detail.orderId().eq(order.id()))
                .leftJoin(DoorHouse.class,(detail,order,h)-> detail.houseId().eq(h.id()))
                .leftJoin(SysUser.class, (d,order, h, u) -> u.userId().eq(h.landlordId()))
                .leftJoin(SysDept.class, (d,order, h, u, d1) -> h.country().eq(d1.deptId()))
                .leftJoin(SysDept.class, (d,order, h, u, d1, d2) -> h.province().eq(d2.deptId()))
                .leftJoin(SysDept.class, (d,order, h, u, d1, d2, d3) -> h.city().eq(d3.deptId()))
                .leftJoin(SysDept.class, (d,order, h, u, d1, d2, d3, d4) -> h.district().eq(d4.deptId()))
                .leftJoin(SysDept.class, (d, order,h, u, d1, d2, d3, d4, d5) -> h.street().eq(d5.deptId()))
                .leftJoin(SysDept.class, (d,order, h, u, d1, d2, d3, d4, d5, d6) -> h.village().eq(d6.deptId()))

                .where((d,order, h, u, d1, d2, d3, d4, d5, d6) -> {
                    if (ObjectUtil.isNotNull(userId)){
                       d.userId().eq(userId);
                    }
                    if (ObjectUtil.isNotNull(doorOrderDetail.getType())){
                        order.type().eq(doorOrderDetail.getType());
                    }
                    if (ObjectUtil.isNotNull(doorOrderDetail.getLandlordName())) {
                        u.nickName().like( doorOrderDetail.getLandlordName() );
                    }
                    if (ObjectUtil.isNotNull(doorOrderDetail.getLandlordPhone())) {
                        u.phonenumber().like( doorOrderDetail.getLandlordPhone() );
                    }
                })
                .orderBy((d,order, h, u, d1, d2, d3, d4, d5, d6)->{
                    d.createTime().desc();
                })
                .select((d,order, h, u, d1, d2, d3, d4, d5, d6) -> {
                    DoorOrderDetailRespVOProxy voProxy=new DoorOrderDetailRespVOProxy();
                    voProxy.selectAll(d);
                    voProxy.country().set(h.country());
                    voProxy.province().set(h.province());
                    voProxy.city().set(h.city());
                    voProxy.village().set(h.village());
                    voProxy.district().set(h.district());
                    voProxy.street().set(h.street());
                    voProxy.startDate().set(h.startDate());
                    voProxy.endDate().set(h.endDate());
                    voProxy.landlordId().set(h.landlordId());
                    voProxy.monthPrice().set(h.monthPrice());
                    voProxy.address().set(h.address());
                    voProxy.door().set(h.door());
                    voProxy.type().set(order.type());
                    voProxy.endDate().set(h.endDate());
                    voProxy.countryName().set(d1.deptName());
                    voProxy.provinceName().set(d2.deptName());
                    voProxy.cityName().set(d3.deptName());
                    voProxy.districtName().set(d4.deptName());
                    voProxy.streetName().set(d5.deptName());
                    voProxy.villageName().set(d6.deptName());
                    voProxy.address().set(h.address());
                    voProxy.landlordName().set(u.nickName());
                    voProxy.landlordPhone().set(u.phonenumber());
                    return voProxy;

                });

        return select;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param doorOrderDetail 【请填写功能名称】
     * @return 结果
     */
    public long insertDoorOrderDetail(DoorOrderDetail doorOrderDetail) {
        doorOrderDetail.setCreateTime(DateUtils.getNowDate());
        return easyEntityQuery.insertable(doorOrderDetail).executeRows();
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param doorOrderDetail 【请填写功能名称】
     * @return 结果
     */
    public long updateDoorOrderDetail(DoorOrderDetail doorOrderDetail) {
        return easyEntityQuery.updatable(doorOrderDetail).executeRows();
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    public long deleteDoorOrderDetailByIds(Long[] ids) {
        return easyEntityQuery.deletable(DoorOrderDetail.class)
                .disableLogicDelete()//禁用逻辑删除,使用物理删除
                .allowDeleteStatement(true)//如果不允
                .where(x -> x.id().in(ids))
                .executeRows();
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    public long deleteDoorOrderDetailById(Long id) {
        return easyEntityQuery.deletable(DoorOrderDetail.class)
                .disableLogicDelete()//禁用逻辑删除,使用物理删除
                .allowDeleteStatement(true)//如果不允
                .where(x -> x.id().eq(id))
                .executeRows();

    }

}